﻿Public Class UserGroupsForm

    Private Sub UserGroupsForm_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

        FillGroupGrid()
        FillFormGrid()
        FillProviderTypesGrid()
        FillUsersGridView()
    End Sub

    Private Sub FillGroupGrid()

        Dim SQL As String
        Dim TheView As DataView

        SQL = "select Group_ID as [Group ID], Group_Name as [Group Name] " & _
              "from Groups"

        TheView = LoadDataView(SQL)

        GroupGrid.DataSource = TheView

        'GroupGrid.Columns("Group ID").Visible = False

    End Sub

    Private Sub FillFormGrid()

        Dim SQL As String
        Dim TheView As DataView
        Dim GroupID As String

        If GroupGrid.SelectedRows.Count = 0 Then Exit Sub

        GroupID = GroupGrid.CurrentRow.Cells("Group ID").Value

        SQL = "select F.Form_Name as [Form Name], " & _
                "case GFA.Access_Level when 'N' then 'No Access' when 'R' then 'Read Only' when 'W' then 'Write Access'end as [Access Level] " & _
                "from dbo.Group_Form_Access GFA " & _
                "left outer join dbo.Forms F on GFA.Form_ID = F.FORM_ID where Group_ID = '" & GroupID & "' " & _
                "union " & _
                "select	Form_Name as [Form Name], " & _
                "case Default_Access_Level when 'N' then 'No Access' when 'R' then 'Read Only' when 'W' then 'Write Access'end as [Access Level] " & _
                "from dbo.Forms " & _
                "where Form_ID not in (select Form_ID from dbo.Group_Form_Access where Group_ID = '" & GroupID & "')"

        TheView = LoadDataView(SQL)

        FormGrid.DataSource = TheView

        FormGrid.DefaultCellStyle.SelectionBackColor = Color.White
        FormGrid.DefaultCellStyle.SelectionForeColor = Color.Black


    End Sub

    Private Sub FillProviderTypesGrid()

        Dim SQL As String
        Dim TheView As DataView
        Dim GroupID As String

        If GroupGrid.SelectedRows.Count = 0 Then Exit Sub

        GroupID = GroupGrid.CurrentRow.Cells("Group ID").Value

        SQL = "select P.Provider_Type_Description as [Provider Type] " & _
                "from Provider_Type_Groups PG " & _
                "left outer join dbo.Provider_Types P on PG.Provider_Type_Code = P.Provider_Type_Code " & _
                "where Group_ID = '" & GroupID & "'"

        TheView = LoadDataView(SQL)

        ProviderTypesGrid.DataSource = TheView

        ProviderTypesGrid.DefaultCellStyle.SelectionBackColor = Color.White
        ProviderTypesGrid.DefaultCellStyle.SelectionForeColor = Color.Black


    End Sub

    Private Sub FillUsersGridView()

        Dim SQL As String
        Dim TheView As DataView
        Dim GroupID As String

        If GroupGrid.SelectedRows.Count = 0 Then Exit Sub

        GroupID = GroupGrid.CurrentRow.Cells("Group ID").Value

        SQL = "select User_Name as [User Name]" & _
                "from Users_Groups " & _
                "where Group_ID = '" & GroupID & "'"

        TheView = LoadDataView(SQL)

        UsersGrid.DataSource = TheView

        UsersGrid.DefaultCellStyle.SelectionBackColor = Color.White
        UsersGrid.DefaultCellStyle.SelectionForeColor = Color.Black

    End Sub


    Private Sub GroupGrid_CellEnter(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles GroupGrid.CellEnter
        FillFormGrid()
        FillProviderTypesGrid()
        FillUsersGridView()

    End Sub

    Private Sub AddButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles AddButton.Click

        If UserFormCheck(Me.Name) <> "W" Then
            MessageBox.Show("Currently you do not have permission to add groups." & vbNewLine & _
                            "Contact TheStore.EFA@education.gsi.gov.uk to change permissions.", "Add Group", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
            Exit Sub
        End If

        GroupEditDialog.ClearForm()

        Dim SQL As String
        Dim TheView As DataView
        SQL = "select max(substring(Group_ID,4,len(Group_ID)))+1 as digit from Groups"
        TheView = LoadDataView(SQL)

        With GroupEditDialog
            .Group_IDTextBox.Text = "GID" & TheView.Item(0).Item("digit").ToString
            .Group_IDTextBox.Enabled = False
        End With

        If GroupEditDialog.ShowDialog() = Windows.Forms.DialogResult.OK Then
            GroupEditDialog.SaveForm()
            FillGroupGrid()
            FillFormGrid()
            FillProviderTypesGrid()
            FillUsersGridView()
        End If
        GroupEditDialog.Dispose()

    End Sub

    Private Sub EditButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles EditButton.Click

        If GroupGrid.SelectedRows.Count = 0 Then Exit Sub

        If UserFormCheck(Me.Name) <> "W" Then
            MessageBox.Show("Currently you do not have permission to edit groups." & vbNewLine & _
                            "Contact TheStore.EFA@education.gsi.gov.uk to change permissions.", "Group Edit", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
            Exit Sub
        End If

        With GroupEditDialog
            .Group_IDTextBox.Text = GroupGrid.CurrentRow.Cells("Group ID").Value
            .Group_IDTextBox.Enabled = False
            .Group_NameTextBox.Text = GroupGrid.CurrentRow.Cells("Group Name").Value
            .LoadForm()
        End With

        If GroupEditDialog.ShowDialog() = Windows.Forms.DialogResult.OK Then
            GroupEditDialog.SaveForm()
            FillGroupGrid()
            FillFormGrid()
            FillProviderTypesGrid()
            FillUsersGridView()
        End If
        GroupEditDialog.Dispose()
    End Sub

    Private Sub DeleteButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles DeleteButton.Click

        If UserFormCheck(Me.Name) <> "W" Then
            MessageBox.Show("Currently you do not have permission to delete groups." & vbNewLine & _
                            "Contact TheStore.EFA@education.gsi.gov.uk to change permissions", "Delete Group", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
            Exit Sub
        End If

        If GroupGrid.SelectedRows.Count = 0 Then Exit Sub

        If MessageBox.Show("Are you sure you want to delete the selected group?", "Delete group?", MessageBoxButtons.YesNo, MessageBoxIcon.Question) = Windows.Forms.DialogResult.No Then
            Exit Sub
        End If


        Dim SQL As String
        Dim GroupID As String

        GroupID = GroupGrid.CurrentRow.Cells("Group ID").Value

        SQL = "delete from Groups where Group_ID = '" & GroupID & "' " & _
                "delete from User_Groups where Group_ID = '" & GroupID & "' " & _
                "delete from Group_Form_Access where Group_ID = '" & GroupID & "' " & _
                "delete from Provider_Type_Groups where Group_ID = '" & GroupID & "'"

        RunNonQuery(SQL)

        FillGroupGrid()
        FillFormGrid()
        FillProviderTypesGrid()
        FillUsersGridView()

        MessageBox.Show("Group has been deleted", "Group Deleted", MessageBoxButtons.OK, MessageBoxIcon.Information)

    End Sub

    Private Sub UserGroupsForm_Shown(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Shown

        If UserFormCheck(Me.Name) = "N" Then
            Me.Close()
            MessageBox.Show("Currently you do not have permission to access this form." & vbNewLine & _
                            "Contact TheStore.EFA@education.gsi.gov.uk if you need access.", Me.Name & " Access", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
            Exit Sub
        End If

    End Sub
End Class